package com.example.venadaoa;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;


import com.example.venadaoa.base.BaseActivity;
import com.example.venadaoa.datamanager.ServerResult;
import com.example.venadaoa.datamanager.SystemInfo;
import com.example.venadaoa.datamanager.UserInfo;
import com.example.venadaoa.net.Api;
import com.example.venadaoa.net.NetUtils;
import com.example.venadaoa.tool.PreferenceUitl;
import com.example.venadaoa.tool.ToastHelp;
import com.google.gson.Gson;

import java.util.HashSet;
import java.util.Set;

import cn.jpush.android.api.JPushInterface;

public class LoginActivity extends BaseActivity implements OnClickListener {
    private EditText mUserName;
    private EditText mPassWord;
    private LoginActivity context;
    private SharedPreferences mySharedPreferences;
    private Intent mIntent;
//    private JPushMessage mNotification = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        mPageName = "LoginActivity";
        setContentView(R.layout.login_view);
        context = this;
        mIntent = getIntent();

        mUserName = (EditText) findViewById(R.id.login_user);
        mySharedPreferences = getSharedPreferences("niciname",
                Activity.MODE_PRIVATE);
        String name = mySharedPreferences.getString("name", "");
        mUserName.setText(name);

        mPassWord = (EditText) findViewById(R.id.login_password);
        String password = mySharedPreferences.getString("password", "");
        mPassWord.setText(password);
        findViewById(R.id.login_btn).setOnClickListener(this);

        //检测系统版本
        //UpdateManager manager = new UpdateManager(context);
        // 检查软件更新
        //manager.checkUpdate();

//		//透明状态栏
//        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//        //透明导航栏
//        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);


//        mNotification = (JPushMessage) getIntent().getSerializableExtra(Constants.NOTIFICATION);

    }

    private boolean checkPasswordAndUsername(String password, String username) {
        if (TextUtils.isEmpty(password) || TextUtils.isEmpty(username)) {
            //Toast.makeText(this, "用户名或者密码不能为空", Toast.LENGTH_SHORT).show();
            ToastHelp.doToastInTop(this, "用户名或者密码不能为空");
            return false;
        }

        //实例化SharedPreferences.Editor对象（第二步）
        SharedPreferences.Editor editor = mySharedPreferences.edit();
        editor.putString("name", username);
        editor.putString("password", password);
        //提交当前数据
        editor.commit();


        return true;
    }


    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        int tag = arg0.getId();
        switch (tag) {
            case R.id.login_btn:

                if (checkPasswordAndUsername(mPassWord.getText().toString(), mUserName.getText().toString())) {

                    //Toast.makeText(LoginActivity.this, "开始登录", Toast.LENGTH_SHORT).show();


                    Api.getLogin(LoginActivity.this, mUserName.getText().toString(), mPassWord.getText().toString(), JPushInterface.getRegistrationID(VenadaOAApplication.getInstance()), UserInfo.class, new NetUtils.NetCallBack<ServerResult>() {
                        @Override
                        public void success(ServerResult rspData) {

                            UserInfo userInfo = (UserInfo) rspData;
                            if (userInfo.getStatus().equals(SUCCESS)) {
                                userInfo.setLogin(true);
                                PreferenceUitl.getInstance(context).saveString(USER_INFO, new Gson().toJson(userInfo));
                                if (!userInfo.getUserObject().getDepartment().equals("洗车部")) {
                                    Set<String> sets = new HashSet<>();
                                    sets.add("c_android");
                                    sets.add("c");
                                    sets.add(userInfo.getUserObject().getDepartmentId());
                                    JPushInterface.setAliasAndTags(VenadaOAApplication.getInstance(), userInfo.getUserObject().getId().replaceAll("-", "_"), sets);
                                }
                                getSystemInfo();
                                Intent intent = new Intent(LoginActivity.this, MenuActivity.class);
                                if (null != mIntent && mIntent.hasExtra(NOTIFICATION)) {
                                    intent.putExtra(NOTIFICATION, mIntent.getSerializableExtra(NOTIFICATION));
                                    mIntent = null;

                                }
                                startActivity(intent);
                            } else if (userInfo.getStatus().equals("error")) {

                                ToastHelp.doToastInTop(LoginActivity.this, userInfo.getMsg());
                            } else {
                                ToastHelp.doToastInTop(LoginActivity.this, "登陆错误，请重试");
                            }
                        }

                        @Override
                        public void failed(String msg) {
                            ToastHelp.doToastInTop(LoginActivity.this, msg);
                        }
                    });

                }
                break;

            default:
                break;
        }

    }


    private void getSystemInfo() {
        Api.getSystemInfo(context, SystemInfo.class, new NetUtils.NetCallBack<ServerResult>() {
            @Override
            public void success(ServerResult rspData) {
                SystemInfo systemInfo = (SystemInfo) rspData;
                String jsonResult = new Gson().toJson(systemInfo);
                if (!jsonResult.contains("error")) {
                    PreferenceUitl.getInstance(context).saveString(SYSTEM_INFO, jsonResult);
                }

            }

            @Override
            public void failed(String msg) {

            }
        });
    }


}
